"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.genProColumnToColumn = genProColumnToColumn;

var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));

var _proField = require("@ant-design/pro-field");

var _proUtils = require("@ant-design/pro-utils");

var _antdV = require("antd-v4");

var _columnRender = require("./columnRender");

var _index = require("./index");

/**
 * 转化 columns 到 pro 的格式 主要是 render 方法的自行实现
 *
 * @param columns
 * @param map
 * @param columnEmptyText
 */
function genProColumnToColumn(params) {
  var columns = params.columns,
      counter = params.counter,
      columnEmptyText = params.columnEmptyText,
      type = params.type,
      editableUtils = params.editableUtils,
      _params$rowKey = params.rowKey,
      rowKey = _params$rowKey === void 0 ? 'id' : _params$rowKey,
      _params$childrenColum = params.childrenColumnName,
      childrenColumnName = _params$childrenColum === void 0 ? 'children' : _params$childrenColum;
  var subNameRecord = new Map();
  return columns.map(function (columnProps, columnsIndex) {
    var key = columnProps.key,
        dataIndex = columnProps.dataIndex,
        valueEnum = columnProps.valueEnum,
        _columnProps$valueTyp = columnProps.valueType,
        valueType = _columnProps$valueTyp === void 0 ? 'text' : _columnProps$valueTyp,
        children = columnProps.children,
        onFilter = columnProps.onFilter,
        _columnProps$filters = columnProps.filters,
        filters = _columnProps$filters === void 0 ? [] : _columnProps$filters;
    var columnKey = (0, _index.genColumnKey)(key || (dataIndex === null || dataIndex === void 0 ? void 0 : dataIndex.toString()), columnsIndex); // 这些都没有，说明是普通的表格不需要 pro 管理

    var noNeedPro = !valueEnum && !valueType && !children;

    if (noNeedPro) {
      return (0, _objectSpread2.default)({
        index: columnsIndex
      }, columnProps);
    }
    /**
     * 是不是展开行和多选按钮
     */


    var isExtraColumns = columnProps === _antdV.Table.EXPAND_COLUMN || columnProps === _antdV.Table.SELECTION_COLUMN;

    if (isExtraColumns) {
      return {
        index: columnsIndex,
        isExtraColumns: true,
        hideInSearch: true,
        hideInTable: false,
        hideInForm: true,
        hideInSetting: true,
        extraColumn: columnProps
      };
    }

    var config = counter.columnsMap[columnKey] || {
      fixed: columnProps.fixed
    };

    var genOnFilter = function genOnFilter() {
      if (onFilter === true) {
        return function (value, row) {
          return (0, _columnRender.defaultOnFilter)(value, row, dataIndex);
        };
      }

      return (0, _proUtils.omitBoolean)(onFilter);
    };

    var keyName = rowKey;
    var tempColumns = (0, _objectSpread2.default)((0, _objectSpread2.default)({
      index: columnsIndex,
      key: columnKey
    }, columnProps), {}, {
      title: (0, _columnRender.renderColumnsTitle)(columnProps),
      valueEnum: valueEnum,
      filters: filters === true ? (0, _proField.proFieldParsingValueEnumToArray)((0, _proUtils.runFunction)(valueEnum, undefined)).filter(function (valueItem) {
        return valueItem && valueItem.value !== 'all';
      }) : filters,
      onFilter: genOnFilter(),
      fixed: config.fixed,
      width: columnProps.width || (columnProps.fixed ? 200 : undefined),
      children: columnProps.children ? genProColumnToColumn((0, _objectSpread2.default)((0, _objectSpread2.default)({}, params), {}, {
        columns: columnProps === null || columnProps === void 0 ? void 0 : columnProps.children
      })) : undefined,
      render: function render(text, rowData, index) {
        if (typeof rowKey === 'function') {
          keyName = rowKey(rowData, index);
        }

        var uniqueKey;

        if (Reflect.has(rowData, keyName)) {
          var _rowData$childrenColu;

          uniqueKey = rowData[keyName];
          var parentInfo = subNameRecord.get(uniqueKey) || [];
          (_rowData$childrenColu = rowData[childrenColumnName]) === null || _rowData$childrenColu === void 0 ? void 0 : _rowData$childrenColu.forEach(function (item) {
            var itemUniqueKey = item[keyName];

            if (!subNameRecord.has(itemUniqueKey)) {
              subNameRecord.set(itemUniqueKey, parentInfo.concat([index, childrenColumnName]));
            }
          });
        }

        var renderProps = {
          columnProps: columnProps,
          text: text,
          rowData: rowData,
          index: index,
          columnEmptyText: columnEmptyText,
          counter: counter,
          type: type,
          subName: subNameRecord.get(uniqueKey),
          editableUtils: editableUtils
        };
        return (0, _columnRender.columnRender)(renderProps);
      }
    });
    return (0, _proUtils.omitUndefinedAndEmptyArr)(tempColumns);
  }).filter(function (item) {
    return !item.hideInTable;
  });
}